Emit rust constants for property names Test: atest sysprop_test Bug: 394074539 Change-Id: I086ea36de498e8f57903316c8afc8ae628766909 
diff --git a/RustGen.cpp b/RustGen.cpp index 5344a79..11aa966 100644 --- a/RustGen.cpp +++ b/RustGen.cpp 
@@ -199,6 +199,13 @@  std::string prop_id =  CamelCaseToSnakeCase(ApiNameToIdentifier(prop.api_name()));   + std::string prop_const = ToUpper(std::string(prop_id)) + "_PROP"; + + // Create constant. + writer.Write("/// The property name of the \"%s\" API.\n", prop_id.c_str()); + writer.Write("pub const %s: &str = \"%s\";\n\n", prop_const.c_str(), + prop.prop_name().c_str()); +  // Create enum.  if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) {  auto enum_type = GetRustEnumType(prop); @@ -271,8 +278,8 @@  identifier.c_str(), prop_return_type.c_str());  writer.Indent();  // Try original property. - writer.Write("let result = match system_properties::read(\"%s\") {\n", - prop.prop_name().c_str()); + writer.Write("let result = match system_properties::read(%s) {\n", + prop_const.c_str());  writer.Indent();  writer.Write("Err(e) => Err(SysPropError::FetchError(e)),\n");  writer.Write( @@ -331,9 +338,8 @@  write_arg = "value.as_str()";  }  writer.Write( - "system_properties::write(\"%s\", " - "%s).map_err(SysPropError::SetError)\n", - prop.prop_name().c_str(), write_arg.c_str()); + "system_properties::write(%s, %s).map_err(SysPropError::SetError)\n", + prop_const.c_str(), write_arg.c_str());  writer.Dedent();  writer.Write("}\n\n");  } 
diff --git a/tests/RustGenTest.cpp b/tests/RustGenTest.cpp index a3e8909..24e32f4 100644 --- a/tests/RustGenTest.cpp +++ b/tests/RustGenTest.cpp 
@@ -111,9 +111,12 @@  use std::fmt;  use rustutils::system_properties::{self, error::SysPropError, parsers_formatters};   +/// The property name of the "test_int" API. +pub const TEST_INT_PROP: &str = "android.test_int"; +  /// Returns the value of the property 'android.test_int' if set.  pub fn test_int() -> std::result::Result<Option<i32>, SysPropError> { - let result = match system_properties::read("android.test_int") { + let result = match system_properties::read(TEST_INT_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -124,12 +127,15 @@  /// Sets the value of the property 'android.test_int', returns 'Ok' if successful.  pub fn set_test_int(v: i32) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format(&v); - system_properties::write("android.test_int", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_INT_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_string" API. +pub const TEST_STRING_PROP: &str = "android.test.string"; +  /// Returns the value of the property 'android.test.string' if set.  pub fn test_string() -> std::result::Result<Option<String>, SysPropError> { - let result = match system_properties::read("android.test.string") { + let result = match system_properties::read(TEST_STRING_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -143,9 +149,12 @@  }  }   +/// The property name of the "test_boo_lea_n" API. +pub const TEST_BOO_LEA_N_PROP: &str = "ro.android.test.b"; +  /// Returns the value of the property 'ro.android.test.b' if set.  pub fn test_boo_lea_n() -> std::result::Result<Option<bool>, SysPropError> { - let result = match system_properties::read("ro.android.test.b") { + let result = match system_properties::read(TEST_BOO_LEA_N_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_bool(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -156,12 +165,15 @@  /// Sets the value of the property 'ro.android.test.b', returns 'Ok' if successful.  pub fn set_test_boo_lea_n(v: bool) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_bool(&v); - system_properties::write("ro.android.test.b", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_BOO_LEA_N_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "android_os_test_long" API. +pub const ANDROID_OS_TEST_LONG_PROP: &str = "android_os_test-long"; +  /// Returns the value of the property 'android_os_test-long' if set.  pub fn android_os_test_long() -> std::result::Result<Option<i64>, SysPropError> { - let result = match system_properties::read("android_os_test-long") { + let result = match system_properties::read(ANDROID_OS_TEST_LONG_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -172,12 +184,15 @@  /// Sets the value of the property 'android_os_test-long', returns 'Ok' if successful.  pub fn set_android_os_test_long(v: i64) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format(&v); - system_properties::write("android_os_test-long", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(ANDROID_OS_TEST_LONG_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_list_int" API. +pub const TEST_LIST_INT_PROP: &str = "test_list_int"; +  /// Returns the value of the property 'test_list_int' if set.  pub fn test_list_int() -> std::result::Result<Option<Vec<i32>>, SysPropError> { - let result = match system_properties::read("test_list_int") { + let result = match system_properties::read(TEST_LIST_INT_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -188,13 +203,16 @@  /// Sets the value of the property 'test_list_int', returns 'Ok' if successful.  pub fn set_test_list_int(v: &[i32]) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_list(v); - system_properties::write("test_list_int", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_LIST_INT_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_strlist" API. +pub const TEST_STRLIST_PROP: &str = "test_strlist"; +  /// Returns the value of the property 'test_strlist' if set.  #[deprecated]  pub fn test_strlist() -> std::result::Result<Option<Vec<String>>, SysPropError> { - let result = match system_properties::read("test_strlist") { + let result = match system_properties::read(TEST_STRLIST_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -206,7 +224,7 @@  #[deprecated]  pub fn set_test_strlist(v: &[String]) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_list(v); - system_properties::write("test_strlist", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_STRLIST_PROP, value.as_str()).map_err(SysPropError::SetError)  }    )"; @@ -222,9 +240,12 @@  use std::fmt;  use rustutils::system_properties::{self, error::SysPropError, parsers_formatters};   +/// The property name of the "test_double" API. +pub const TEST_DOUBLE_PROP: &str = "android.test_double"; +  /// Returns the value of the property 'android.test_double' if set.  pub fn test_double() -> std::result::Result<Option<f64>, SysPropError> { - let result = match system_properties::read("android.test_double") { + let result = match system_properties::read(TEST_DOUBLE_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -235,12 +256,15 @@  /// Sets the value of the property 'android.test_double', returns 'Ok' if successful.  pub fn set_test_double(v: f64) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format(&v); - system_properties::write("android.test_double", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_DOUBLE_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_int" API. +pub const TEST_INT_PROP: &str = "android.test_int"; +  /// Returns the value of the property 'android.test_int' if set.  pub fn test_int() -> std::result::Result<Option<i32>, SysPropError> { - let result = match system_properties::read("android.test_int") { + let result = match system_properties::read(TEST_INT_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -251,12 +275,15 @@  /// Sets the value of the property 'android.test_int', returns 'Ok' if successful.  pub fn set_test_int(v: i32) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format(&v); - system_properties::write("android.test_int", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_INT_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_string" API. +pub const TEST_STRING_PROP: &str = "android.test.string"; +  /// Returns the value of the property 'android.test.string' if set.  pub fn test_string() -> std::result::Result<Option<String>, SysPropError> { - let result = match system_properties::read("android.test.string") { + let result = match system_properties::read(TEST_STRING_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -270,6 +297,9 @@  }  }   +/// The property name of the "test_enum" API. +pub const TEST_ENUM_PROP: &str = "android.test.enum"; +  #[allow(missing_docs)]  #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Hash, Ord)]  pub enum TestEnumValues { @@ -315,7 +345,7 @@    /// Returns the value of the property 'android.test.enum' if set.  pub fn test_enum() -> std::result::Result<Option<TestEnumValues>, SysPropError> { - let result = match system_properties::read("android.test.enum") { + let result = match system_properties::read(TEST_ENUM_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -326,12 +356,15 @@  /// Sets the value of the property 'android.test.enum', returns 'Ok' if successful.  pub fn set_test_enum(v: TestEnumValues) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format(&v); - system_properties::write("android.test.enum", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_ENUM_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_boo_lea_n" API. +pub const TEST_BOO_LEA_N_PROP: &str = "ro.android.test.b"; +  /// Returns the value of the property 'ro.android.test.b' if set.  pub fn test_boo_lea_n() -> std::result::Result<Option<bool>, SysPropError> { - let result = match system_properties::read("ro.android.test.b") { + let result = match system_properties::read(TEST_BOO_LEA_N_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_bool(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -342,12 +375,15 @@  /// Sets the value of the property 'ro.android.test.b', returns 'Ok' if successful.  pub fn set_test_boo_lea_n(v: bool) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_bool(&v); - system_properties::write("ro.android.test.b", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_BOO_LEA_N_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "android_os_test_long" API. +pub const ANDROID_OS_TEST_LONG_PROP: &str = "android_os_test-long"; +  /// Returns the value of the property 'android_os_test-long' if set.  pub fn android_os_test_long() -> std::result::Result<Option<i64>, SysPropError> { - let result = match system_properties::read("android_os_test-long") { + let result = match system_properties::read(ANDROID_OS_TEST_LONG_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -358,12 +394,15 @@  /// Sets the value of the property 'android_os_test-long', returns 'Ok' if successful.  pub fn set_android_os_test_long(v: i64) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format(&v); - system_properties::write("android_os_test-long", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(ANDROID_OS_TEST_LONG_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_double_list" API. +pub const TEST_DOUBLE_LIST_PROP: &str = "test_double_list"; +  /// Returns the value of the property 'test_double_list' if set.  pub fn test_double_list() -> std::result::Result<Option<Vec<f64>>, SysPropError> { - let result = match system_properties::read("test_double_list") { + let result = match system_properties::read(TEST_DOUBLE_LIST_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -374,12 +413,15 @@  /// Sets the value of the property 'test_double_list', returns 'Ok' if successful.  pub fn set_test_double_list(v: &[f64]) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_list(v); - system_properties::write("test_double_list", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_DOUBLE_LIST_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_list_int" API. +pub const TEST_LIST_INT_PROP: &str = "test_list_int"; +  /// Returns the value of the property 'test_list_int' if set.  pub fn test_list_int() -> std::result::Result<Option<Vec<i32>>, SysPropError> { - let result = match system_properties::read("test_list_int") { + let result = match system_properties::read(TEST_LIST_INT_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -390,13 +432,16 @@  /// Sets the value of the property 'test_list_int', returns 'Ok' if successful.  pub fn set_test_list_int(v: &[i32]) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_list(v); - system_properties::write("test_list_int", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_LIST_INT_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "test_strlist" API. +pub const TEST_STRLIST_PROP: &str = "test_strlist"; +  /// Returns the value of the property 'test_strlist' if set.  #[deprecated]  pub fn test_strlist() -> std::result::Result<Option<Vec<String>>, SysPropError> { - let result = match system_properties::read("test_strlist") { + let result = match system_properties::read(TEST_STRLIST_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -408,9 +453,12 @@  #[deprecated]  pub fn set_test_strlist(v: &[String]) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_list(v); - system_properties::write("test_strlist", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(TEST_STRLIST_PROP, value.as_str()).map_err(SysPropError::SetError)  }   +/// The property name of the "el" API. +pub const EL_PROP: &str = "el"; +  #[allow(missing_docs)]  #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Hash, Ord)]  pub enum ElValues { @@ -445,7 +493,7 @@  /// Returns the value of the property 'el' if set.  #[deprecated]  pub fn el() -> std::result::Result<Option<Vec<ElValues>>, SysPropError> { - let result = match system_properties::read("el") { + let result = match system_properties::read(EL_PROP) {  Err(e) => Err(SysPropError::FetchError(e)),  Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),  Ok(None) => Ok(None), @@ -457,7 +505,7 @@  #[deprecated]  pub fn set_el(v: &[ElValues]) -> std::result::Result<(), SysPropError> {  let value = parsers_formatters::format_list(v); - system_properties::write("el", value.as_str()).map_err(SysPropError::SetError) + system_properties::write(EL_PROP, value.as_str()).map_err(SysPropError::SetError)  }    )";